// 21.合并两个有序链表
package LeetcodeHot100;


public class Solution021 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode pre = new ListNode(0);
        ListNode result = pre;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                ListNode temp = list1.next;
                list1.next = pre.next;
                pre.next = list1;
                pre = pre.next;
                list1 = temp;
            } else {
                ListNode temp = list2.next;
                list2.next = pre.next;
                pre.next = list2;
                pre = pre.next;
                list2 = temp;
            }
        }
        if (list1 != null)
            pre.next = list1;
        if (list2 != null)
            pre.next = list2;
        return result.next;
    }
}
